package com.sys.SysFeedingRecords.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.enums.BusinessType;
import com.sys.SysFeedingRecords.domain.SysInventory;
import com.sys.SysFeedingRecords.service.ISysInventoryService;
import com.sys.common.utils.poi.ExcelUtil;
import com.sys.common.core.page.TableDataInfo;

/**
 * 物资库存Controller
 * 
 * @author sys
 * @date 2025-09-19
 */
@RestController
@RequestMapping("/SysFeedingRecords/SysInventory")
public class SysInventoryController extends BaseController
{
    @Autowired
    private ISysInventoryService sysInventoryService;

    /**
     * 查询物资库存列表
     */
    @PreAuthorize("@ss.hasPermi('SysFeedingRecords:SysInventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysInventory sysInventory)
    {
        startPage();
        List<SysInventory> list = sysInventoryService.selectSysInventoryList(sysInventory);
        return getDataTable(list);
    }

    /**
     * 导出物资库存列表
     */
    @PreAuthorize("@ss.hasPermi('SysFeedingRecords:SysInventory:export')")
    @Log(title = "物资库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysInventory sysInventory)
    {
        List<SysInventory> list = sysInventoryService.selectSysInventoryList(sysInventory);
        ExcelUtil<SysInventory> util = new ExcelUtil<SysInventory>(SysInventory.class);
        util.exportExcel(response, list, "物资库存数据");
    }

    /**
     * 获取物资库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('SysFeedingRecords:SysInventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysInventoryService.selectSysInventoryById(id));
    }

    /*
    * 三级联动查询
    * */
    /**
     * 获取所有物资类别（用于一级联动）
     */
    @GetMapping("/categories")
    public AjaxResult listCategories() {
        List<String> categories = sysInventoryService.selectCategoryList();
        return success(categories);
    }

    /**
     * 根据类别获取物资名称列表（二级联动）
     */
    @GetMapping("/materials")
    public AjaxResult listMaterialsByCategory(String category) {
        if (category == null || category.trim().isEmpty()) {
            return AjaxResult.error("请选择物资类别");
        }
        List<String> materialNames = sysInventoryService.selectMaterialNamesByCategory(category);
        return success(materialNames);
    }

    /**
     * 根据类别和物资名称获取批次号列表（三级联动）
     */
    @GetMapping("/batches")
    public AjaxResult listBatchesByMaterial(@RequestParam String category,
                                            @RequestParam String materialName) {
        if (category == null || category.trim().isEmpty()) {
            return AjaxResult.error("物资类别不能为空");
        }
        if (materialName == null || materialName.trim().isEmpty()) {
            return AjaxResult.error("物资名称不能为空");
        }
        List<String> batches = sysInventoryService.selectBatchNumbersByMaterialAndCategory(category, materialName);
        return success(batches);
    }

    /**
     * 根据类别和物资名称获取批次号列表（四级联动）
     */
    /**
     * 根据类别和物资名称获取批次号列表（四级联动）
     */
    @GetMapping("/record-ids")
    public AjaxResult listBatchesByBatchNumber(@RequestParam String category,
                                               @RequestParam String materialName,
                                               @RequestParam String batchNumber) {
        if (category == null || category.trim().isEmpty()) {
            return AjaxResult.error("物资类别不能为空");
        }
        if (materialName == null || materialName.trim().isEmpty()) {
            return AjaxResult.error("物资名称不能为空");
        }
        if (batchNumber == null || batchNumber.trim().isEmpty()) {
            return AjaxResult.error("物资编号不能为空");
        }

        List<Map<String, Object>> batchesWithIds = sysInventoryService.selectBatchNumbersWithIdsByMaterialAndCategory(category, materialName, batchNumber);

        // 将结果转换为JSON格式返回
        return success(batchesWithIds);
    }

    /**
     * 新增物资库存
     */
    @PreAuthorize("@ss.hasPermi('SysFeedingRecords:SysInventory:add')")
    @Log(title = "物资库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysInventory sysInventory)
    {
        return toAjax(sysInventoryService.insertSysInventory(sysInventory));
    }

    /**
     * 修改物资库存
     */
    @PreAuthorize("@ss.hasPermi('SysFeedingRecords:SysInventory:edit')")
    @Log(title = "物资库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysInventory sysInventory)
    {
        return toAjax(sysInventoryService.updateSysInventory(sysInventory));
    }

    /**
     * 删除物资库存
     */
    @PreAuthorize("@ss.hasPermi('SysFeedingRecords:SysInventory:remove')")
    @Log(title = "物资库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysInventoryService.deleteSysInventoryByIds(ids));
    }
}
